package com.liuqi.iot.web;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.liuqi.common.base.bean.query.DynamicQuery;
import com.liuqi.iot.bean.dto.DeviceModelDTO;
import com.liuqi.iot.bean.dto.DeviceModelFieldDTO;
import com.liuqi.iot.bean.query.DeviceModelQuery;
import com.liuqi.iot.service.DeviceModelEntityService;
import com.liuqi.iot.service.DeviceModelService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * 设备模型控制器 
 * @author Coder Generator 2025-03-22 15:13:29 
 **/
@RestController
@RequestMapping("/manager/device-model")
@Slf4j
@Tag(name = "设备模型控制器")
public class DeviceModelController {
    @Autowired
    private DeviceModelEntityService service;

    @Autowired
    private DeviceModelService deviceModelService;

    @PostMapping("add")
    @Operation(summary = "新增")
    public void add(@RequestBody @Validated DeviceModelDTO req) {
        deviceModelService.save(req);
    }

    @PutMapping("update")
    @Operation(summary = "更新")
    public void update(@RequestBody @Validated DeviceModelDTO req) {
        deviceModelService.save(req);
    }

    @PostMapping("save")
    @Operation(summary = "保存")
    public DeviceModelDTO save(@RequestBody @Validated DeviceModelDTO req) {
        return deviceModelService.save(req);
    }

    @DeleteMapping("delete/{id}")
    @Operation(summary = "删除")
    public void delete(@PathVariable("id") String id) {
        service.delete(id);
    }

    @GetMapping("detail/{id}")
    @Operation(summary = "根据id查找记录")
    public DeviceModelDTO findById(@PathVariable("id") String id) {
        return deviceModelService.findById(id).orElse(null);
    }

    @PostMapping("page-query")
    @Operation(summary = "查询-分页")
    public IPage<DeviceModelDTO> pageQuery(@RequestBody DeviceModelQuery query) {
        return service.pageQuery(query);
    }

    @PostMapping("filter")
    @Operation(summary = "查询-动态")
    public IPage<DeviceModelDTO> pageQuery(@RequestBody DynamicQuery query) {
        return service.dynamicPageQuery(query);
    }

    @PostMapping("query")
    @Operation(summary = "查询-不分页")
    public List<DeviceModelDTO> query(@RequestBody DeviceModelQuery query) {
        return service.query(query);
    }

    @GetMapping("publish")
    public void publish(String id) {
        deviceModelService.publish(id);
    }

    @GetMapping("offline")
    public void offline(String id) {
        deviceModelService.offline(id);
    }

    @GetMapping("fields")
    public List<DeviceModelFieldDTO> getModelFields(String modelId) {
        return deviceModelService.getModelFields(modelId);
    }

    @GetMapping("import/template")
    public void downloadTemplate() {
        deviceModelService.downloadTemplate();
    }

    @GetMapping("import")
    public void importData(
            String categoryId,
            MultipartFile file) {
        deviceModelService.importData(categoryId, file);
    }
}